package com.example.demo.common.utils;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.util.List;
import java.util.Map;

public class ExcelUnit {

    public static HSSFWorkbook updownp (List rowsName, List<Map<String,Object>> dataList,List list ){


        //创建excel工作簿
        HSSFWorkbook workbook=new HSSFWorkbook();
        //创建工作表sheet
        HSSFSheet sheet=workbook.createSheet();
        //创建第一行
        HSSFRow row=sheet.createRow(0);
        HSSFCell cell=null;
        //插入第一行数据的表头
        for(int i=0;i<rowsName.size();i++){
            cell=row.createCell(i);
            cell.setCellValue(rowsName.get(i).toString());
        }
        //写入数据
        for (int i=1;i<=dataList.size();i++){
            Map<String,Object> map = dataList.get(i-1);
            HSSFRow nrow=sheet.createRow(i);
            HSSFCell ncell;
            for (int j=0;j<list.size();j++){
                 ncell=nrow.createCell(j);
                if (map.get(list.get(j).toString()) ==null || "".equals(map.get(list.get(j).toString()))){
                    ncell.setCellValue("");
                }else {
                    ncell.setCellValue(map.get(list.get(j).toString()).toString());
                }

            }
        }
        return workbook;

    }


}
